{extend name="v1/layout/base"}
{block name="content"}
<div class="layui-row layui-col-space15">
  <div class="layui-col-md12">
    <form class="layui-card-body layui-form layui-form-pane">
      <div class="layui-row layui-col-space10 layui-form-item">
        <div class="layui-col-lg8">
          <label class="layui-form-label">管理组名称：</label>
          <div class="layui-input-block">
            <input type="text" name="group_name" lay-verify="required" lay-reqtext="请输入管理组名称" placeholder="请输入管理组名称" autocomplete="off" class="layui-input">
          </div>
        </div>
      </div>
      <div class="layui-row layui-col-space10 layui-form-item">
        <div class="layui-col-lg8">
          <label class="layui-form-label">管理组权限：</label>
          <div class="layui-input-block">
            <div id="auth_rule" class="demo-tree-more"></div>
          </div>
        </div>
      </div>
      <div class="layui-row layui-col-space10 layui-form-item">
        <div class="layui-col-lg8">
          <label class="layui-form-label">管理组描述：</label>
          <div class="layui-input-block">
            <textarea name="des" rows="8" cols="80" id="des"></textarea>
          </div>
        </div>
      </div>
      <div class="layui-row layui-col-space10 layui-form-item">
        <div class="layui-col-lg8">
          <label class="layui-form-label">是否显示：</label>
          <div class="layui-input-block">
            <input type="radio" name="status" value="1" title="显示" checked>
            <input type="radio" name="status" value="0" title="不显示">
          </div>
        </div>
      </div>
      <!-- 底部操作按钮 -->
      <div class="layui-row">
        <div class="layui-col-xs3">&nbsp;</div>
        <div class="layui-col-xs4">
          <input type="hidden" name="act" value="create" />
          <button type="button" class="layui-btn layui-btn-primary layui-btn-radius" id="backList"><i class="layui-icon">&#xe65c;</i>返回列表</button>
          <button type="button" class="layui-btn layui-btn-normal layui-btn-radius" lay-submit lay-filter="saveBtn"><i class="layui-icon">&#xe609;</i>立即更新</button>
        </div>
        <div class="layui-col-xs5">&nbsp;</div>
      </div>
      <!-- 底部操作按钮结束 -->
    </form>
  </div>
</div>
{/block}
{block name="myScript"}
<script>
  layui.use(['form', 'tree'], function() {
    var form = layui.form,
      tree = layui.tree,
      layer = layui.layer,
      url = "/{:config('manage.manage_name')}/{:config('manage.version')}/Admin/getAuthRuleTree",
      $ = layui.$;
    https(url, {}, 'post', true, '正在更新权限节点').then(res => {
      if (res.result_code == 200) {
        tree.render({
          elem: '#auth_rule',
          data: res.resultData,
          id: 'check_auth_rule',
          showCheckbox: true,
          isJump: false //是否允许点击节点时弹出新窗口跳转
        });
      }
    })
    //监听提交
    form.on('submit(saveBtn)', function(data) {
      var checkedData = tree.getChecked('check_auth_rule'); //获取选中节点的数据
      var data = data.field;
      var datas = {};
      datas.group_name = data.group_name;
      datas.des = data.des;
      datas.status = data.status;
      datas.act = data.act;
      datas.auth_rule_id = getChecked_list(checkedData);
      datas.auth_rule_name = getChecked_name_list(checkedData);
      url = "{:url('/'.config('manage.manage_name').'/'.config('manage.version').'/Admin/adminGroupSave')}";
      https(url, datas, 'post', 1, '正在保存信息').then(res => {
        xkMsg(res.result_msg);
      }, fail => {})
      return false;
    });

    function getChecked_list(data) {
      var id = "";
      $.each(data, function(index, item) {
        if (id != "") {
          id = id + "," + item.id;
        } else {
          id = item.id;
        }
        var i = getChecked_list(item.children);
        if (i != "") {
          id = id + "," + i;
        }
      });
      return id;
    }
    function getChecked_name_list(data) {
      var auth_rule_name = "";
      $.each(data, function(index, item) {
        if (auth_rule_name != "") {
          auth_rule_name = auth_rule_name + "," + item.auth_name;
        } else {
          auth_rule_name = item.auth_name;
        }
        var i = getChecked_name_list(item.children);
        if (i != "") {
          auth_rule_name = auth_rule_name + "," + i;
        }
      });
      return auth_rule_name;
    }
  });
</script>
{/block}
